package com.xci.edu.module.foreground.pc.train.controller;

import com.github.lvyanyang.annotation.AllowAnonymous;
import com.github.lvyanyang.core.BaseWebController;
import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.XCI;
import com.xci.edu.core.entity.EduStudent;
import com.xci.edu.core.service.TrainStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/train")
public class TrainDefaultController extends BaseWebController {

    /** 学员培训登录 */
    @Resource
    private TrainStudentService trainStudentService;

    @GetMapping("/login")
    public String login() {
        BoolResult result=trainStudentService.getStudentJwtCookie();
        if(result.isSuccess()){
            return "redirect:/train/index";
        }else{
            //判断之前有没有登录过
            if(!XCI.isEmpty(trainStudentService.getCurrentStudent())){
                //登录过
                return "redirect:/train/index";
            }else{
                //没登陆过
                return "foreground/pc/train/login";
            }
        }
    }

    /**
     *
     * @param account   账号
     * @param password  密码
     * @param autoLogin 下次是否自动登录
     * @param vcode 验证码
     * @param map
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/loginHandle")
    @AllowAnonymous
    public BoolResult loginHandle(String account, String password,boolean autoLogin,String vcode, ModelMap map) {
        return trainStudentService.login(account,password,autoLogin,vcode);
    }

    /**
     * 修改密码页面
     */
    @GetMapping("/modifyPassword")
    public String modifyPassword() {
        return "foreground/pc/train/modifyPassword";
    }

    /** 修改密码保存 */
    @ResponseBody
    @PostMapping("/modifyPasswordSave")
    public BoolResult modifyPassword(String currentPwd, String newPwd) {
        EduStudent eduStudent = trainStudentService.getCurrentStudent();
        return trainStudentService.modifyPassword(eduStudent.getId(), currentPwd, newPwd);
    }

    /** 安全退出 */
    @GetMapping("/logout")
    public String logout() {
        trainStudentService.logout();
        return "redirect:/train/login";
    }

    @GetMapping("/index")
    public String index(ModelMap map){
        return "foreground/pc/train/index";
    }
}
